<?php

namespace App\Http\Logic\PC;

use App;
use App\Helper\Helper;
use Illuminate\Support\Facades\DB;
use App\Models\Business;
use App\Models\Job;
use App\Common\Status;

class JobLogic
{
    public static function getJobs($params, $pageSize, $page)
    {
        $where = [['a.show', '=', Status::PASS], ['a.status', '=', Status::PASS], ['a.is_deleted', '=', Status::NOT_DELETE]];
        if (isset($params['type']) && $params['type']) {
            $where[] = ['b.type', '=', $params['type']];
        }

        if (isset($params['hot']) && $params['hot']) {
            $where[] = ['a.hot', '=', $params['hot']];
        }

        if (isset($params['scale']) && $params['scale']) {
            $where[] = ['b.scale', '=', $params['scale']];
        }

        if (isset($params['bname']) && $params['bname']) {
            $where[] = ["b.name", "like", '%' . $params['bname'] . '%'];

        }

        if (isset($params['position_custom_name']) && $params['position_custom_name']) {
            $where[] = ["a.position_custom_name", "like", '%' . $params['position_custom_name'] . '%'];
        }

        if (isset($params['work_exp']) && $params['work_exp']) {
            $where[] = ['a.work_exp', '=', $params['work_exp']];
        }

        if (isset($params['education']) && $params['education']) {
            $where[] = ['a.education', '=', $params['education']];
        }

        if (isset($params['position_category_child_id']) && $params['position_category_child_id']) {
            $where[] = ['a.position_category_child_id', '=', $params['position_category_child_id']];
        }

        if (isset($params['city']) && $params['city']) {
            $where[] = ['a.city', '=', $params['city']];
        }

        if (isset($params['job_type']) && $params['job_type']) {
            $where[] = ['a.job_type', '=', $params['job_type']];
        }
        if (isset($params['salary']) && $params['salary']) {
            if ($params['salary'] == 1) {
                $where[] = ['a.salary1', '<=', 3000];
            }

            switch ($params['salary']) {
                case 1:
                    $where[] = ['a.salary1', '<=', 3000];
                    break;
                case 2:
                    $where[] = ['a.salary1', '>=', 3000];
                    $where[] = ['a.salary2', '<=', 5000];
                    break;
                case 3:
                    $where[] = ['a.salary1', '>=', 5000];
                    $where[] = ['a.salary2', '<=', 8000];
                    break;
                case 4:
                    $where[] = ['a.salary1', '>=', 8000];
                    $where[] = ['a.salary2', '<=', 10000];
                    break;
                case 5:
                    $where[] = ['a.salary1', '>=', 10000];
                    $where[] = ['a.salary2', '<=', 15000];
                    break;
                case 6:
                    $where[] = ['a.salary1', '>=', 15000];
                    $where[] = ['a.salary2', '<=', 20000];
                    break;
                case 7:
                    $where[] = ['a.salary1', '>=', 20000];
                    $where[] = ['a.salary2', '<=', 30000];
                    break;
                case 8:
                    $where[] = ['a.salary1', '>=', 30000];
                    $where[] = ['a.salary2', '<=', 50000];
                    break;
                case 9:
                    $where[] = ['a.salary1', '>=', 50000];
                    $where[] = ['a.salary2', '<=', 100000];
                    break;
                default:
                    $where[] = ['a.salary2', '>=', 100000];
            }
        }

        $field = [
            "a.id", "a.business_id", "d.name as position_category_child_name",
            "a.position_custom_name", "a.position_category_child_id", "a.work_exp",
            "a.education", "a.salary1", "a.salary2", "a.salary_month", "a.welfare",
            "a.city", "a.area", "a.email", "a.release", "a.release_job", "a.job_type",
            "a.release_mobile", "a.describe", "a.hot", "b.name", "b.short_name",
            "b.logo", "b.scale", "b.type", "b.content", "b.hot", "c.path"
        ];
        $data  = DB::table('recruit_job as a')
            ->leftJoin('recruit_position_category as d', "a.position_category_child_id", "=", "d.id")
            ->leftJoin('recruit_business as b', function ($join) {
                $join->on("a.business_id", "=", "b.id");
            })->leftJoin('recruit_resources as c', function ($join) {
                $join->on("b.logo", "=", "c.id")
                    ->where("c.status", '=', Status::REGULAR)
                    ->where("c.is_deleted", '=', Status::NOT_DELETE);
            })->select($field)->where($where)
            ->orderBy("a.created_at", "desc")
            ->offset($pageSize * ($page - 1))
            ->limit($pageSize)
            ->get();

        $res   = self::getDataInfo($data);
        $count = self::getCount($where);
        return ['list' => $res, 'total' => $count, 'page' => $page, 'pageSize' => $pageSize];
    }


    private static function getCount($where)
    {
        return DB::table('recruit_job as a')
            ->leftJoin('recruit_business as b', function ($join) {
                $join->on("a.business_id", "=", "b.id");
            })->leftJoin('recruit_resources as c', function ($join) {
                $join->on("b.logo", "=", "c.id")
                    ->where("c.status", '=', Status::REGULAR)
                    ->where("c.is_deleted", '=', Status::NOT_DELETE);
            })->where($where)
            ->count();
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public static function getJobsDt($id)
    {
        $field = [
            "a.id", "a.business_id", "a.position_custom_name", "a.position_category_child_id",
            "d.name as position_category_child_name", "a.work_exp",
            "a.education", "a.salary1", "a.salary2", "a.salary_month", "a.welfare",
            "a.city", "a.area", "a.email", "a.release", "a.release_job", "a.job_type",
            "a.release_mobile", "a.describe", "a.hot", "b.name", "b.short_name",
            "b.logo", "b.scale", "b.type", "b.content", "b.hot", "c.path"
        ];
        $where = [
            ['a.show', '=', Status::PASS], ['a.id', '=', $id],
            ['a.is_deleted', '=', Status::NOT_DELETE]
        ];

        $data = DB::table('recruit_job as a')
            ->leftJoin('recruit_business as b', function ($join) {
                $join->on("a.business_id", "=", "b.id");
            })
            ->leftJoin('recruit_position_category as d', function ($join) {
                $join->on("a.position_category_child_id", "=", "d.id");
            })->leftJoin('recruit_resources as c', function ($join) {
                $join->on("b.logo", "=", "c.id")
                    ->where("c.status", '=', Status::REGULAR)
                    ->where("c.is_deleted", '=', Status::NOT_DELETE);
            })->select($field)->where($where)
            ->first();
        //            ->toSql();
        if ($data) {
            $res = self::getDataInfo([$data]);
            //            $res = $res[0];
            //            # 是否投递 并且状态正常 没有查看过
            //            $info         = DB::table("recruit_job_notes_auth")->where([
            //                ["type", '=', 1],
            //                ["job_id", '=', $id],
            //                ["interview", '=', 1],
            //                ["mobile", '=', Helper::pcByMobile()]
            //            ])->first();
            //            $res->deliver = $info ? 1 : 2;

            return $res;
        }
        return [];

    }

    public static function getDataInfo($data)
    {
        foreach ($data as $datum) {
            $area_name        = DB::table('recruit_city')->select("name")->where("id", $datum->area)->first();
            $city_name        = DB::table('recruit_city')->select(["parentid", "name"])->where("id", $datum->city)
                ->first();
            $datum->area_name = $area_name ? $area_name->name : '';
            $datum->city_name = $city_name ? $city_name->name : '';

            $datum->work_exp_info  = Status::WORK_EXP[$datum->work_exp];
            $datum->job_type_info  = Status::JOB_TYPE[$datum->job_type];
            $datum->education_info = Status::EDUCATION[$datum->education];
            $datum->hot_info       = $datum->hot == 1 ? '否' : '是';

            # 企业
            $datum->business_hot_info = $datum->hot == 1 ? '否' : '是';
            $datum->business_scale    = Status::BUSINESS_SCALE[$datum->scale];
            $datum->business_type     = Status::BUSINESS_TYPE[$datum->type];
            $datum->welfare           = explode(',', $datum->welfare);
            $datum->path              = Helper::buildQiNiuUrl($datum->path);

            # 在招聘职位数量
            $datum->num = DB::table('recruit_job')->where(
                [
                    ["business_id","=",$datum->business_id],
                    ["status", '=', Status::PASS],
                    ["is_deleted", '=', Status::NOT_DELETE]
                ])->count();

        }
        return $data;
    }


    /**
     * @param $id
     * @return bool
     */
    public static function setDeliverJobs($id)
    {
        # todo 暂定 只写入一次记录
        //        # 获取用户简历
        //        $rInfo = DB::table('recruit_notes_middle as a')
        //            ->leftJoin('recruit_resources as b', function ($join) {
        //                $join->on("a.res_id", "=", "b.id")
        //                    ->where("b.status", '=', Status::REGULAR)
        //                    ->where("b.is_deleted", '=', Status::NOT_DELETE);
        //            })->select(["b.id"])->where("a.mobile", Helper::pcByMobile())->first();
        //

        # 是否已投递
        $ag = DB::table("recruit_job_notes_auth")->where(
            [
                ["type", "=", 1],
                ["job_id", "=", $id],
                ["mobile", "=", Helper::pcByMobile()],
            ]
        )->first();
        if (!$ag) {
            $data['created_at'] = time();
            $data['type']       = 1;
            $data['mobile']     = Helper::pcByMobile();
            $data['job_id']     = $id;
            //        $data['res_id']     = $rInfo->id;

            $jn_id = DB::table("recruit_job_notes_auth")->insertGetId($data);

            # 添加到记录
            $dataRecord['created_at']   = time();
            $dataRecord['job_notes_id'] = $jn_id;
            $dataRecord['source']       = 1;
            $dataRecord['interview']    = 7;
            return DB::table("recruit_job_notes_record")->insert($dataRecord);
        }
    }


    /**
     * 获取投递记录
     * @param $type
     * @return mixed
     */
    public static function getRecruitJobNotes($type)
    {
        $field = [
            "a.id as auth_id", "c.id", "c.position_custom_name", "c.position_category_child_id", "c.work_exp",
            "c.education", "c.salary1", "c.salary2", "c.salary_month", "c.welfare", "c.status",
            "c.city", "c.area", "c.email", "c.release", "c.release_job", "c.job_type", "c.business_id",
            "c.release_mobile", "c.describe", "c.hot", "d.name", "d.short_name",
            "d.logo", "d.scale", "d.type", "d.content", "d.hot", "e.path", "f.id as note_id"
        ];
        $where = [["a.mobile", '=', Helper::pcByMobile()], ["a.status", '=', Status::REGULAR]];

        # 查看
        if ($type == 1) {
            $where[] = ["a.interview", ">", 1];
        }
        # 推荐
        if ($type == 2) {
            $where[] = ["a.sell", "=", 2];
        }
        # 不合适
        if ($type == 3) {
            $where[] = ["a.interview", "=", 4];
        }
        # 感兴趣
        if ($type == 4) {
            $where[] = ["a.interest", "=", 2];
        }
        # 邀面试
        if ($type == 5) {
            $where[] = ["a.interview", "=", 2];
        }
        # 停止招聘
        if ($type == 6) {
            $where[] = ["c.status", "=", 3];
        }

        $data = DB::table('recruit_job_notes_auth as a')
            ->leftJoin('recruit_job as c', function ($join) {
                $join->on("a.job_id", "=", "c.id");
            })->leftJoin('recruit_business as d', function ($join) {
                $join->on("c.business_id", "=", "d.id");
            })->leftJoin('recruit_notes as f', function ($join) {
                $join->on("f.mobile", "=", "a.mobile");
            })->leftJoin('recruit_resources as e', function ($join) {
                $join->on("d.logo", "=", "e.id")
                    ->where("e.status", '=', Status::REGULAR)
                    ->where("e.is_deleted", '=', Status::NOT_DELETE);
            })->select($field)->where($where)
            ->orderBy("a.created_at","desc")
            ->get();

        # 获取最新一条记录
        foreach ($data as $datum) {
            $datum->record_status = $datum->created_at = "";
            $rec                  = DB::table('recruit_job_notes_record')->where("job_notes_id", $datum->auth_id)
                ->select(["created_at", "interview"])
                ->orderBy("created_at", "desc")
                ->first();
            if ($rec) {
                $datum->record_status     = $rec->interview;
                $datum->record_created_at = $rec->created_at;
            }

            $datum->interview = [];
            # 获取面试信息
            $old              = DB::table('recruit_job_interview')->where(
                [
                    "status"       => Status::REGULAR,
                    "is_deleted"   => Status::NOT_DELETE,
                    "job_id"       => $datum->id,
                    "job_notes_id" => $datum->note_id,
                ])->first();
            $datum->interview = $old ?: [];
        }
        return self::getDataInfo($data);
    }


    /**
     * 投递记录详情
     * @param $recruit_id
     * @return mixed
     */
    public static function getRecruitJobNotesDtl($recruit_id)
    {
        $field = [
            "a.id as job_notes_id", "a.reason", "c.id as recruit_id", "c.position_custom_name", "c.position_category_child_id", "c.work_exp",
            "c.education", "c.salary1", "c.salary2", "c.salary_month", "c.welfare",
            "c.city", "c.area", "c.email", "c.release", "c.release_job", "c.job_type", "c.business_id",
            "c.release_mobile", "c.describe", "c.hot", "d.name", "d.short_name",
            "d.logo", "d.scale", "d.type", "d.content", "d.hot", "e.path"
        ];
        $where = [["a.mobile", '=', Helper::pcByMobile()], ["a.job_id", '=', $recruit_id]];

//        DB::connection()->enableQueryLog();

        $data = DB::table('recruit_job_notes_auth as a')
            ->leftJoin('recruit_job as c', function ($join) {
                $join->on("a.job_id", "=", "c.id");
            })->leftJoin('recruit_business as d', function ($join) {
                $join->on("c.business_id", "=", "d.id");
            })->leftJoin('recruit_resources as e', function ($join) {
                $join->on("d.logo", "=", "e.id")
                    ->where("c.status", '=', Status::REGULAR)
                    ->where("c.is_deleted", '=', Status::NOT_DELETE);
            })->select($field)->where($where)
            ->get();
//        dd(DB::getQueryLog());
//        var_dump("====");
//        var_dump($data);
        if (!$data->isEmpty()) {
            $info = self::getDataInfo($data);
            $info = $info[0];
            # 获取记录
            $rec          = DB::table('recruit_job_notes_record')
                ->where("job_notes_id", $info->job_notes_id)
                ->orderBy("created_at", "desc")
                ->get();
            $info->record = [];
            if ($rec->isNotEmpty()) {
                foreach ($rec as $item) {
                    # 记录描述
                    switch ($item->interview) {
                        case 2:
                            $describe = "业务部门邀约面试";
                            break;
                        case 3:
                            $describe = "部门负责人已查看简历";
                            break;
                        case 4:
                            $describe = $data[0]->reason ?: "";
                            break;
                        case 5:
                            $describe = "已推荐版图负责人";
                            break;
                        case 7:
                            $describe = "待查看";
                            break;
                        case 8:
                            $describe = "部门负责人已对您的简历标记为感兴趣";
                            break;
                        case 9:
                            $describe = "部门负责人已取消对您简历的感兴趣";
                            break;
                        case 10:
                            $describe = "部门负责人已取消对您简历的不合适";
                            break;
                        default:
                            $describe = "已为您匹配该岗位";
                    }
                    $item->describe       = $describe;
                    $item->interview_name = Status::DELIVER_STATUS[$item->interview];
                }
                $info->record = $rec;
            }
            return $info;
        }
        return [];
    }
}
